<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="玻璃长" prop="glassLength">
        <el-input
          v-model="queryParams.glassLength"
          placeholder="请输入玻璃长"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="玻璃宽" prop="glassWidth">
        <el-input
          v-model="queryParams.glassWidth"
          placeholder="请输入玻璃宽"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="玻璃厚" prop="glassHeight">
        <el-input
          v-model="queryParams.glassHeight"
          placeholder="请输入玻璃厚"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="体积" prop="volume">
        <el-input
          v-model="queryParams.volume"
          placeholder="请输入体积"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="订单价格" prop="orderPrice">
        <el-input
          v-model="queryParams.orderPrice"
          placeholder="请输入订单价格"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="用户姓名" prop="customerName">
        <el-input
          v-model="queryParams.customerName"
          placeholder="请输入用户姓名"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="用户手机" prop="customerContact">
        <el-input
          v-model="queryParams.customerContact"
          placeholder="请输入用户手机"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="创建时间" prop="createdAt">
        <el-date-picker clearable
          v-model="queryParams.createdAt"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="请选择创建时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="更新时间" prop="updatedAt">
        <el-date-picker clearable
          v-model="queryParams.updatedAt"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="请选择更新时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['system:orders:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['system:orders:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['system:orders:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['system:orders:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="ordersList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="${comment}" align="center" prop="id" />
      <el-table-column label="房屋类型" align="center" prop="houseType" />
      <el-table-column label="吊装类型" align="center" prop="liftingType" />
      <el-table-column label="玻璃类型" align="center" prop="glassType" />
      <el-table-column label="玻璃长(米)" align="center" prop="glassLength" />
      <el-table-column label="玻璃宽(米)" align="center" prop="glassWidth" />
      <el-table-column label="玻璃厚(厘米)" align="center" prop="glassHeight" />
      <el-table-column label="重量(千克)" align="center" prop="volume" />
      <el-table-column label="订单价格(元)" align="center" prop="orderPrice" />
      <el-table-column label="用户姓名" align="center" prop="customerName" />
      <el-table-column label="用户手机" align="center" prop="customerContact" />
      <el-table-column label="订单状态" align="center" prop="orderStatus" />
      <el-table-column label="创建时间" align="center" prop="createdAt" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createdAt, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="更新时间" align="center" prop="updatedAt" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.updatedAt, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['system:orders:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:orders:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改订单管理对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="房屋类型" prop="houseType">
          <el-select v-model="form.houseType" placeholder="请选择房屋类型">
            <el-option
              v-for="item in ['洋房', '别墅']"
              :key="item"
              :label="item"
              :value="item">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="吊装类型" prop="liftingType">
          <el-select v-model="form.liftingType" placeholder="请选择吊装类型">
            <el-option
              v-for="item in ['玻璃', '家具', '家电']"
              :key="item"
              :label="item"
              :value="item">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="玻璃类型" prop="glassType">
          <el-select v-model="form.glassType" placeholder="请选择玻璃类型">
            <el-option
              v-for="item in ['单层', '双面', '夹胶中空']"
              :key="item"
              :label="item"
              :value="item">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="玻璃长" prop="glassLength">
          <el-input v-model="form.glassLength" placeholder="请输入玻璃长" />
        </el-form-item>
        <el-form-item label="玻璃宽" prop="glassWidth">
          <el-input v-model="form.glassWidth" placeholder="请输入玻璃宽" />
        </el-form-item>
        <el-form-item label="玻璃厚" prop="glassHeight">
          <el-input v-model="form.glassHeight" placeholder="请输入玻璃厚" />
        </el-form-item>


        <el-form-item label="用户姓名" prop="customerName">
          <el-input v-model="form.customerName" placeholder="请输入用户姓名" />
        </el-form-item>
        <el-form-item label="用户手机" prop="customerContact">
          <el-input v-model="form.customerContact" placeholder="请输入用户手机" />
        </el-form-item>

      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
  import { listOrders, getOrders, delOrders, addOrders, updateOrders } from "@/api/system/orders";

  export default {
    name: "Orders",
    data() {
      return {
        // 遮罩层
        loading: true,
        // 选中数组
        ids: [],
        // 非单个禁用
        single: true,
        // 非多个禁用
        multiple: true,
        // 显示搜索条件
        showSearch: true,
        // 总条数
        total: 0,
        // 订单管理表格数据
        ordersList: [],
        // 弹出层标题
        title: "",
        // 是否显示弹出层
        open: false,
        // 查询参数
        queryParams: {
          pageNum: 1,
          pageSize: 10,
          houseType: null,
          liftingType: null,
          glassType: null,
          glassLength: null,
          glassWidth: null,
          glassHeight: null,
          volume: null,
          orderPrice: null,
          customerName: null,
          customerContact: null,
          orderStatus: null,
          createdAt: null,
          updatedAt: null
        },
        // 表单参数
        form: {
          id: null,
          houseType: null,
          liftingType: null,
          glassType: null,
          glassLength: null,
          glassWidth: null,
          glassHeight: null,
          volume: null,
          orderPrice: null,
          customerName: null,
          customerContact: null,
          orderStatus: null,
          createdAt: null,
          updatedAt: null
        },
        // 表单校验
        rules: {
          houseType: [
            { required: true, message: "房屋类型不能为空", trigger: "change" }
          ],
          liftingType: [
            { required: true, message: "吊装类型不能为空", trigger: "change" }
          ],
          glassType: [
            { required: true, message: "玻璃类型不能为空", trigger: "change" }
          ],
          glassLength: [
            { required: true, message: "玻璃长不能为空", trigger: "blur" }
          ],
          glassWidth: [
            { required: true, message: "玻璃宽不能为空", trigger: "blur" }
          ],
          glassHeight: [
            { required: true, message: "玻璃厚不能为空", trigger: "blur" }
          ],
          customerName: [
            { required: true, message: "用户姓名不能为空", trigger: "blur" }
          ],
          customerContact: [
            { required: true, message: "用户手机不能为空", trigger: "blur" }
          ],
          createdAt: [
            { required: true, message: "创建时间不能为空", trigger: "blur" }
          ],
          updatedAt: [
            { required: true, message: "更新时间不能为空", trigger: "blur" }
          ]
        }
      };
    },
  created() {
    this.getList();
  },
  methods: {
    /** 查询订单管理列表 */
    getList() {
      this.loading = true;
      listOrders(this.queryParams).then(response => {
        this.ordersList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        houseType: null,
        liftingType: null,
        glassType: null,
        glassLength: null,
        glassWidth: null,
        glassHeight: null,
        volume: null,
        orderPrice: null,
        customerName: null,
        customerContact: null,
        orderStatus: null,
        createdAt: null,
        updatedAt: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加订单管理";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getOrders(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改订单管理";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateOrders(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addOrders(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除订单管理编号为"' + ids + '"的数据项？').then(function() {
        return delOrders(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/orders/export', {
        ...this.queryParams
      }, `orders_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
